import api from '../../../common/api.js'
import { getData } from '../../../models/transferData.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    houseName: '',
    markers: {},
    isDisabled: true, //保存按钮是否禁用
  },
  //小区名称
  getHouseName(e) {
    this.setData({
      houseName: e.detail.value
    })
    if (e.detail.value.length <= 0 || this.data.markers.address == undefined){
       this.setData({
         isDisabled: true
       });
    } else {
      this.setData({
        isDisabled: false
      });
    }
  },
  //选择位置
  chooseLocation() {
    const that = this;
    wx.chooseLocation({
      success: function (res) {
        if(res.length!=0){
          that.setData({
            markers: res,
            isDisabled: false
          })
        }else{
          that.setData({
            markers: res,
            isDisabled: true
          })
        }
      },
    })
  },
  //下一步
  goInfoEntryHouse() {
    const that = this;
    // debugger
    if (that.data.markers.address == "" || that.data.markers.address == undefined){
      wx.showToast({
        title: '小区位置不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    api.addVillage({
      resblockName: that.data.houseName,
      addr: that.data.markers.address,
      lng: that.data.markers.longitude,
      lat: that.data.markers.latitude,
    }, (res) => {
      console.log(res.data.content.id)
      let villageData = {
        resblockName: that.data.houseName,
        resblockId:res.data.content.id,
        addr: that.data.markers.address,
      }

      let pageUrl = getData('selectHouseAreaEnterNextPage') || '/pages/addHouse/infoEntryHouse/infoEntryHouse';
      wx.navigateTo({
        url: `${pageUrl}?villageData=${JSON.stringify(villageData)}`
      })
    })
  },
  //保存主营楼盘
  saveMainBuilding(){
    wx.setStorageSync("setState", true);
    const that = this;
    api.addVillage({
      resblockName: that.data.houseName,
      addr: that.data.markers.address,
      lng: that.data.markers.longitude,
      lat: that.data.markers.latitude,
    }, res => {
      // if (res.data.isSuccess) {
        var data = [{ resblockId: res.data.content.id, hdResblockId: res.data.content.hdResblockId}];
        api.editResblock({ resblock:JSON.stringify(data)},res=>{
          var loginUserInfo = getApp().globalData.loginUserInfo;
          if (loginUserInfo.data.content.sessionUser.extProps.resblock.length == 0) {
            loginUserInfo.data.content.sessionUser.extProps.resblock.push({});
          }
          loginUserInfo.data.content.sessionUser.extProps.resblock[0].city = res.data.content[0].city;
          loginUserInfo.data.content.sessionUser.extProps.resblock[0].area = res.data.content[0].area;
          wx.navigateBack({
            detal: 2
          })
        })
      // }
    })
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setStorageSync('lufangtiaozhuan', 2)
    if (options.keywords){
      this.setData({
        houseName: options.keywords
      })
    }
    if (options.mainBuilding) {
      this.setData({
        mainBuilding: options.mainBuilding
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})
